public class TestMap {

    int[] elem = new int[10];

    public void InitMap(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            elem[i] = arr[i];
            usedSize++;
        }
    }
    public int usedSize;


    //建立大根堆
    public void createMap(){

        for (int parent = (usedSize-1-1)/2; parent>=0 ; parent--) {

            shiftDown(parent,usedSize);
        }
    }

    private void shiftDown(int parent, int usedSize) {

        int child = 2*parent+1;  //对应左孩子（从0开始的）
        while (child<usedSize) {
            if (child + 1 < usedSize && elem[child] < elem[child + 1]) {
                child++;
            }
            if (elem[parent] < elem[child]) {
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
                parent = child;
                child = 2 * parent + 1;
            }else{
                break;
            }
        }


    }
}
